#pragma once

#include "../source/tcp_server.hpp"

class EchoServer
{
public:
    EchoServer(uint16_t port)
    :_tcp_server(port)
    {
        _tcp_server.SetThreadCount(2);
        _tcp_server.OpenTimeout(10);
        _tcp_server.SetClosedCallback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
        _tcp_server.SetConnectedCallback(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
        _tcp_server.SetMessageCallback(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
    }

    void Start()
    {
        _tcp_server.Start();
    }

private:
    void OnConnected(const ConnectionPtr &conn)
    {
        LOG(LogLevel::DEBUG) << " OnConnected conn.get() : " << conn.get();
    }
    void OnMessage(const ConnectionPtr &conn, Buffer *buf)
    {
        conn->Send(buf->ReadPtr(), buf->ReadSize());
        buf->ReadIndexSkewing(buf->ReadSize());
        //conn->ShutDwon();
    }

    void OnClosed(const ConnectionPtr &conn)
    {
        LOG(LogLevel::DEBUG) << " OnClosed conn.get() : " << conn.get();
    }

private:
    TcpServer _tcp_server;
};
